package com.superteam.mac.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.superteam.mac.mode.IndexRec;

public class IndexRecDAO extends BaseDAO {

	@SuppressWarnings("unchecked")
	public List<IndexRec> findByConnection(String workshop,String machineCode,String startTime,String endTime)
	{
		String sql = "select * from tbIndexRec where 1=1 ";
		if(null!=workshop&&!"".equals(workshop))
		{
			sql += " and WorkshopCode = '"+workshop+"'";
		}
		if(null!=machineCode&&!"".equals(machineCode))
		{
			sql += " and machineCode = '"+machineCode+"'";
		}
		if(null!=startTime&&!"".equals(startTime))
		{
			sql += " and RecTime > '"+startTime+"'";
		}
		if(null!=endTime&&!"".equals(endTime))
		{
			sql += " and RecTime < '"+endTime+"'";
		}
		
		String newSql = "SELECT * FROM "+
						"( "+
						"SELECT A.*, ROWNUM RN  "+
						"FROM ("+sql+") A  "+
						"WHERE ROWNUM <= 40  "+
						")  "+
						"WHERE RN >= 0 ";
		List<IndexRec>  list = this.getJdbcTemplate().query(
				   newSql,
				    new RowMapper() {

				        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				        	IndexRec index = new IndexRec();
				        	index.setIndexCode(rs.getString("IndexName"));
				        	index.setMachineCode(rs.getString("MachineCode"));
				        	index.setWorkShopCode(rs.getString("WorkshopCode"));
				        	index.setRecTime(rs.getString("RecTime"));
				        	index.setRecVal(rs.getString("RecVal"));
						        	
				            return index;
				        }
				    });
			return list;
	}
}
